const { regClass, property } = Laya;

@regClass()
export class RoomBox extends Laya.Script {
    //declare owner : Laya.Sprite3D;
    declare owner: Laya.GBox;

    onEnable(): void {

    }

    openAllDoors(duration = 300, callback?: Laya.Handler) {
        let count = 0;
        for (let i = 0; i < 8; i++) {
            let panel = this.owner.getChildByName(`room${i}`).getChildByName<Laya.GPanel>("panel");
            let img = panel.getChildByName<Laya.GImage>("img");
            if (img == void 0)
                return;
            if (i == 0) {
                img.x = 60;
                Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 1) {
                img.x = 0;
                Laya.Tween.create(img).duration(duration).to("x", 88).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 2) {
                img.x = 0;
                Laya.Tween.create(img).duration(duration).to("x", 100).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 3) {
                img.y = 0;
                Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 4) {
                img.x = 0;
                Laya.Tween.create(img).duration(duration).to("x", 97).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 5) {
                img.y = 0;
                Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 6) {
                img.y = 0;
                Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 7) {
                img.x = 0;
                Laya.Tween.create(img).duration(duration).to("x", 97).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            }
        }
    }

    closeAllDoors(duration = 300, callback?: Laya.Handler) {
        let count = 0;
        for (let i = 0; i < 8; i++) {
            let panel = this.owner.getChildByName(`room${i}`).getChildByName<Laya.GPanel>("panel");
            let img = panel.getChildByName<Laya.GImage>("img");
            if (img == void 0)
                return;
            if (i == 0) {
                img.x = 0;
                Laya.Tween.create(img).duration(duration).to("x", 60).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 1) {
                img.x = 88;
                Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 2) {
                img.x = 100;
                Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 3) {
                img.y = -46;
                Laya.Tween.create(img).duration(duration).to("y", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 4) {
                img.x = 97;
                Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 5) {
                img.y = -46;
                Laya.Tween.create(img).duration(duration).to("y", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 6) {
                img.y = -46;
                Laya.Tween.create(img).duration(duration).to("y", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            } else if (i == 7) {
                img.x = 97;
                Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                    count++;
                    if (count >= 8) {
                        callback && callback.run();
                    }
                }, this);
            }
        }
    }

    openDoor(roomIndex: number, duration: number, callback?: Laya.Handler) {
        console.log("open door " + roomIndex);
        let panel = this.owner.getChildByName(`room${roomIndex}`).getChildByName<Laya.GPanel>("panel");
        let img = panel.getChildByName<Laya.GImage>("img");
        if (img == void 0)
            return;

        if (roomIndex == 0) {
            img.x = 60;
            Laya.Tween.create(img).duration(duration).to("x", 0).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 1) {
            img.x = 0;
            Laya.Tween.create(img).duration(duration).to("x", 88).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 2) {
            img.x = 0;
            Laya.Tween.create(img).duration(duration).to("x", 100).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 3) {
            img.y = 0;
            Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 4) {
            img.x = 0;
            Laya.Tween.create(img).duration(duration).to("x", 100).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 5) {
            img.y = 0;
            Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 6) {
            img.y = 0;
            Laya.Tween.create(img).duration(duration).to("y", -46).then(() => {
                callback && callback.run();
            }, this);
        } else if (roomIndex == 7) {
            img.x = 0;
            Laya.Tween.create(img).duration(duration).to("x", 100).then(() => {
                callback && callback.run();
            }, this);
        }
    }
}